* Adjust the `github' macro to match the local path

local github "/Users/eborbath/Documents/GitHub/jobbik-paper/replication/"

*********************************
*** Analysis in the main text ***
*********************************

use "`github'/data/quarterly_withIVs.dta", clear

tostring(quarterly), replace

split quarterly, p(".")
destring quarterly1, replace
destring quarterly2, replace

gen edate=yq(quarterly1, quarterly2)
format edate %tq

drop quarterly*

gen lifecycle=.
replace lifecycle=1 if edate<tq(2010q2)
replace lifecycle=2 if edate>=tq(2010q2) & edate<tq(2015q3)
replace lifecycle=3 if edate>=tq(2015q3)

lab def lifecycle 1 "pre-parliament" 2 "In parliament (>2010q2 & < 2015q3)" 3 "Migration crisis (>2015q3)"
lab val lifecycle lifecycle

gen data_source2=.
replace data_source2=1 if data_source==5
replace data_source2=2 if data_source==1
replace data_source2=3 if data_source==2
replace data_source2=4 if data_source==3
replace data_source2=5 if data_source==4

lab def data_source2 1 "Press Releases" 2 "Kuruc.info" 3 "Alfahir.hu" 4 "FB - Jobbik page" 5 "FB - Vona page"
lab val data_source2 data_source2
drop data_source
rename data_source2 data_source

lab var nativism_f_pm_fidesz "Fidesz' nativism"
lab var populism "Jobbik's populism"
label var Jobbik "Jobbik's popularity"

sort data_source edate

xtset data_source edate
*tsline populism, by(data_source)

* M1

xtpcse nativism i.data_source populism i.lifecycle l.Jobbik l.undecided_prop l.nativism_f_pm_fidesz , correlation(psar1)
est sto M1

* M2

xtpcse nativism l.c.nativism_f_pm_fidesz##i.data_source populism i.lifecycle l.Jobbik l.undecided_prop, correlation(psar1)
est sto M2

* M3

xtpcse nativism i.data_source##l.c.undecided_prop populism i.lifecycle l.Jobbik l.nativism_f_pm_fidesz , correlation(psar1)
est sto M3

* M4

xtpcse nativism populism i.lifecycle l.c.Jobbik##l.c.undecided_prop##i.data_source l.nativism_f_pm_fidesz , correlation(psar1)
est sto M4

* regression table

esttab M1 M2 M3 M4 ///
using "`github'/TS_regressions/tables/table_1.html", ///
b(2) label interact(" * ") nogaps scalars(r2) ///
nobaselevels noomitted noconstant nonumbers /// 
mtitles("Model 1" "Model 2" "Model 3" "Model 4") ///
nonote replace 

* marginal effect figures

est restore M2

****************************
*** Appendix B, figure 1 ***
****************************

margins, at(c.l.nativism_f_pm_fidesz=(0(0.05)0.6) data_source=(1(1)5))
marginsplot, by(data_source) byopt(title("")) plot1opts(lpattern("solid") lwidth("medium") lcolor("black") mlcolor("black") mfcolor("black") msymbol(O)) ///
ci1opts(lpattern("shortdash_dot") lwidth("medium")  lcolor("black")) recastci(rline) xtitle("Fidesz' nativism") ylabel(0(.2)1) yscale(range(0 1))
*graph save "`github'/TS_regressions/margins/ds_fidesznat.gph", replace
graph export "`github'/TS_regressions/margins/app_B_figure_1.png", as(png) width(1200) height(800) replace

est restore M3

****************************
*** Appendix B, figure 2 ***
****************************

margins, at(c.l.undecided_prop=(25(3)53) data_source=(1(1)5))
marginsplot, by(data_source) byopt(title("")) plot1opts(lpattern("solid") lwidth("medium") lcolor("black") mlcolor("black") mfcolor("black") msymbol(O)) ///
ci1opts(lpattern("shortdash_dot") lwidth("medium")  lcolor("black")) recastci(rline) xtitle("Undecided electorate") ylabel(0(.2)1) yscale(range(0 1))
*graph save "`github'/TS_regressions/margins/ds_undecided.gph", replace
graph export "`github'/TS_regressions/margins/app_B_figure_2.png", as(png) width(1200) height(800) replace

est restore M4

****************
*** Figure 4 ***
****************

margins data_source, at(c.l.undecided_prop=(25 (4) 53) c.l.Jobbik=(5 12) )
marginsplot, by(data_source) byopt(title("")) allsimplelabels ///
plot1opts(lpattern("solid") lwidth("medium") lcolor("gs6") mlcolor("gs6")) ///
plot2opts(lpattern("solid") lwidth("medium") lcolor("black")) ///
ci1opts(lpattern("shortdash_dot") lwidth("medium")  lcolor("black")) ///
ci2opts(lpattern("shortdash_dot") lwidth("medium")  lcolor("black")) ///
recastci(rline) xtitle("Share of undecideds") ///
legend(pos(6) col(3)) ylabel(0(.2)1) yscale(range(0 1))
*graph save "`github'/TS_regressions/margins/ds_Jobbik_undecided.gph", replace
graph export "`github'/TS_regressions/margins/Figure_4.png", as(png) width(1200) height(800) replace

